package cn.java.web.side.validate.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {


    @Bean
    public Docket createRestApi() {
        List<Parameter> pars = new ArrayList<>();

        pars.add(genCommonHeadParameter("x-authenticated-userid","x-authenticated-userid"));

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    // TODO: 2019/4/17 自定义title和描述信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档-swagger")
                .description("接口文档-swagger")
                .build();
    }


    private Parameter genCommonHeadParameter(String s, String desc) {
        ParameterBuilder signature = new ParameterBuilder();
        signature.name(s)
                .description(desc)
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        return signature.build();
    }
}
